<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/05/0005
 * Time: 9:34
 */

namespace app\sports_admin\controller;


use app\exception\PlanException;
use app\sports_admin\model\PlanType;
use think\Request;
use app\sports_admin\service\Plan as PlanService;
use app\sports_admin\service\PlayType as PlanTypeService;
use app\sports_admin\model\Plan as PlanModel;
use app\sports_admin\model\Grade as GradeModel;
use app\sports_admin\model\SportProject as SportProjectModel;
use app\sports_admin\vaildate\Plan as PlanVaildate;
class Plan extends Base
{
//    protected $beforeActionList = [
//        'isResource' => ['only'=>'index,getplaninfo,edit,insert,update,plandelete'],
//        'operationSelect'=>['only'=>'index,getplaninfo,edit'],
//        'operationUpdate'=>['only'=>'update'],
//        'operationInsert'=>['only'=>'insert'],
//        'operationDelete'=>['only'=>'plandelete']
//    ];
    //教案首页
    public function index()
    {
        return view('index');
    }
    /**
     * 获取教案信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
    */
    public function getPlanInfo()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        return PlanModel::getPlanInfo($page,$rows,$sort,$sortOrder,$searchName);
    }
    //添加教案页面
    public function add()
    {
        //获取年级信息
        $grade = GradeModel::field('id,grade_title')->select();
        //运动类别id
        $project = SportProjectModel::field('id,title')->select();
        //分类信息
        $data = PlanTypeService::index();
        return view('add',compact('grade','project','data'));
    }
    /**
     * 添加教案信息
    */
    public function insert(Request $request)
    {
        if($request->isAjax()){
            (new PlanVaildate())->insert();
            return PlanService::planInsert();
        }
    }
    /**
     * 编辑页面
     * @param $id 教案id
     */
    public function edit(Request $request)
    {
        $id = $request->get('id');
        //运动类别id
        $project = SportProjectModel::field('id,title')->select();
        //获取年级信息
        $grade = GradeModel::field('id,grade_title')->select();
        //获取教案类别
        $type = PlanTypeService::index();
        $info = PlanModel::where('id',$id)->find()->toArray();
        $oneImg = $info['start_part']['one_imgs'];
        $twoImg = $info['preparation_part']['two_imgs'];
        $imgStr = $info['basic_part']['str_img'];
        unset($info['start_part']['one_imgs']);
        unset($info['preparation_part']['two_imgs']);
        unset($info['basic_part']['str_img']);

        return view('edit',compact('info','type','grade','imgStr','oneImg','twoImg','project'));
    }
    /**
     * 修改教案信息
    */
    public function update(Request $request)
    {
        if($request->isAjax()){
            (new PlanVaildate())->update();
            return PlanService::planUpdate();
        }
    }
    /**
     * 删除教案
     * @param $id 教案id
    */
    public function planDelete(Request $request)
    {
        if($request->isAjax()){
            $id = intval($request->post('id'));
            $bool = PlanModel::destroy($id);
            if(!$bool){
                throw new PlanException([
                    'msg'=>'删除失败',
                    'errorCode'=>'400'
                ]);
            }
            return json(['code'=>201,'msg'=>'删除成功']);
        }
    }

    public function test()
    {
        $info = PlanModel::where('id',105)->find()->toArray();
        $oneImg = $info['start_part']['one_imgs'];
        $twoImg = $info['preparation_part']['two_imgs'];
        $imgStr = $info['basic_part']['str_img'];
        unset($info['start_part']['one_imgs']);
        unset($info['preparation_part']['two_imgs']);
        unset($info['basic_part']['str_img']);
        foreach($info['start_part'] as &$val) {
            $val['one_activity'] = str_replace('1、','.',$val['one_activity']);
            $val['one_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_activity'])) ;
            $val['one_activity'] = str_replace('2、','.',$val['one_activity']);
            $val['one_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_activity'])) ;
            $val['one_activity'] = str_replace('3、','.',$val['one_activity']);
            $val['one_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_activity'])) ;
            $val['one_activity'] = str_replace('4、','.',$val['one_activity']);
            $val['one_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_activity'])) ;
            $val['one_activity'] = str_replace('5、','.',$val['one_activity']);
            $val['one_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_activity'])) ;
            $val['one_contents'] = str_replace('1、','.',$val['one_contents']);
            $val['one_contents'] = preg_replace("/\t/","",htmlspecialchars($val['one_contents'])) ;
            $val['one_contents'] = str_replace('2、','.',$val['one_contents']);
            $val['one_contents'] = preg_replace("/\t/","",htmlspecialchars($val['one_contents'])) ;
            $val['one_contents'] = str_replace('3、','.',$val['one_contents']);
            $val['one_contents'] = preg_replace("/\t/","",htmlspecialchars($val['one_contents'])) ;
            $val['one_contents'] = str_replace('4、','.',$val['one_contents']);
            $val['one_contents'] = preg_replace("/\t/","",htmlspecialchars($val['one_contents'])) ;
            $val['one_contents'] = str_replace('5、','.',$val['one_contents']);
            $val['one_contents'] = preg_replace("/\t/","",htmlspecialchars($val['one_contents'])) ;
            $val['one_s_activity'] = str_replace('1、','.',$val['one_s_activity']);
            $val['one_s_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_s_activity']));
            $val['one_s_activity'] = str_replace('2、','.',$val['one_s_activity']);
            $val['one_s_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_s_activity']));
            $val['one_s_activity'] = str_replace('3、','.',$val['one_s_activity']);
            $val['one_s_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_s_activity']));
            $val['one_s_activity'] = str_replace('4、','.',$val['one_s_activity']);
            $val['one_s_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_s_activity']));
            $val['one_s_activity'] = str_replace('5、','.',$val['one_s_activity']);
            $val['one_s_activity'] = preg_replace("/\t/","",htmlspecialchars($val['one_s_activity']));
        }
        $info['start_part']['one_imgs'] = $oneImg;
        echo "<pre>";
        print_r($info['start_part']);
        echo "</pre>";
        //var_dump($info['start_part']);
    }
}